package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.pojo.FormTagPo;
import cc.linker.steplink.crm.merchant.service.FormPcService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pc/form")
public class FormPcController {

    @Autowired
    private FormPcService formService;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 新增/编辑
     * @param session
     * @param param
     */
    @PostMapping("/tag/save")
    public void save(HttpServletRequest request, @RequestBody Map<String, Object> param){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId", orgId);
        if(StringUtils.isEmpty(param.get("id"))){
            //新增
            String[] names = {"labelId","formId","attrId","attrTitle","attritemids","attritems"};
            for(String item:names){
                if(StringUtils.isEmpty(param.get(item))){
                    throw GlobalException.build("参数错误（"+item+"）");
                }
            }
            param.put("attritems", JSON.toJSONString(param.get("attritems")));
            String customerId;
            if(userinfo.getSaler()){
                customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
            }else{
                customerId = orgId;
            }
            param.put("customerId",customerId);
            formService.add(param);
        }else{
            //编辑
            if(!StringUtils.isEmpty(param.get("attritems"))){
                param.put("attritems", JSON.toJSONString(param.get("attritems")));
            }
            formService.update(param);
        }

    }

    /**
     * 获取详情
     */
    @GetMapping("/tag/getinfo")
    public FormTagPo getInfo(HttpServletRequest request, Integer id){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>(2);
        param.put("orgId", userinfo.getOrgId());
        param.put("id",id);
        return formService.getInfo(param);
    }

    /**
     * 获取列表
     */
    @GetMapping("/tag/getlist")
    public PageInfo getList(
            HttpServletRequest request,
            @RequestParam(value = "formId") String formId,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize
    ){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String customerId;
        if(userinfo.getSaler()){
            customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
        }else{
            customerId = orgId;
        }
        Map<String,Object> param = new HashMap<>(3);
        param.put("orgId", orgId);
        param.put("customerId", customerId);
        param.put("formId", formId);
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(formService.getList(param));
    }

    /**
     * 删除
     */
    @GetMapping("/tag/delete")
    public void delete(HttpServletRequest request, Integer id){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String customerId;
        if(userinfo.getSaler()){
            customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
        }else{
            customerId = orgId;
        }
        Map<String,Object> param = new HashMap<>(3);
        param.put("id", id);
        param.put("orgId", orgId);
        param.put("customerId", customerId);
        formService.delete(param);
    }


}
